package com.hliushi.spark.sql

import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * descriptions:
 *
 * author: Hliushi
 * date: 2021/5/20 15:55
 */
object WindowFun1 {


  /**
   * 统计每个商品和此品类最贵商品之间的差值
   *
   * @param args
   */
  def main(args: Array[String]): Unit = {
    // 1.创建SparkSession
    val spark: SparkSession = SparkSession.builder()
      .appName("window")
      .master("local[6]")
      .getOrCreate()

    import spark.implicits._

    val source: DataFrame = Seq(
      ("Thin", "Cell phone", 6000),
      ("Norma1", "Tablet", 1500),
      ("Mini", "Tablet", 5500),
      ("Uitra thin", "Cell phone", 5000),
      ("Very thin", "Cell phone", 6000),
      ("Big", "Tablet", 2500),
      ("Bendab1e", "Cell phone", 3000),
      ("Fo1dable", "Cell phone", 3000),
      ("Pro", "Tablet", 4500),
      ("Pro2", "Tablet", 6500)
    ).toDF("product", "category", "revenue")

    import org.apache.spark.sql.functions._

    // 1.定义窗口, 按照分类进行倒序排序
    val windowSpec = Window.partitionBy($"category")
      .orderBy($"revenue".desc)


    // 2.找到最贵的商品价格
    val maxPrice = max($"revenue") over (windowSpec)

    // 3.得到结果
    source.select($"product", $"category", $"revenue", (maxPrice - $"revenue") as "revenue_difference")
      .show()

    //  +----------+----------+-------+------------------+
    //  |   product|  category|revenue|revenue_difference|
    //  +----------+----------+-------+------------------+
    //  |      Thin|Cell phone|   6000|                 0|
    //  | Very thin|Cell phone|   6000|                 0|
    //  |Uitra thin|Cell phone|   5000|              1000|
    //  |  Bendab1e|Cell phone|   3000|              3000|
    //  |  Fo1dable|Cell phone|   3000|              3000|
    //  |      Pro2|    Tablet|   6500|                 0|
    //  |      Mini|    Tablet|   5500|              1000|
    //  |       Pro|    Tablet|   4500|              2000|
    //  |       Big|    Tablet|   2500|              4000|
    //  |    Norma1|    Tablet|   1500|              5000|
    //  +----------+----------+-------+------------------+
  }

}